//app.js
import request from './utils/common/request.js'
import user from './utils/common/user.js'
App({
  globalData: {
    hasMessage: false,
    contactList: [],
    unRead: 0,
    tabBar: {
      "backgroundColor": "#ffffff",
      "color": "#979795",
      "selectedColor": "#1B7AFF",
      "list": [{
          "pagePath": "/pages/home/home",
          "text": "首页",
          "iconPath": "../../image/home1.png",
          "selectedIconPath": "../../image/home2.png"
        },
        {
          "pagePath": "/pages/publish/publish",
          "text": "发布",
          "iconPath": "../../image/publish1.png",
          "selectedIconPath": "../../image/publish2.png"
        },
        {
          "pagePath": "/pages/message/myMessage",
          "text": "消息",
          "iconPath": "../../image/message1.png",
          "selectedIconPath": "../../image/message2.png"
        },
        {
          "pagePath": "/pages/mine/mine",
          "text": "我的",
          "iconPath": "../../image/my1.png",
          "selectedIconPath": "../../image/my2.png"
        }
      ]
    }
  },
  editTabbar: function () {
    //隐藏系统tabbar
    wx.hideTabBar();
    let tabbar = this.globalData.tabBar;
    let currentPages = getCurrentPages();
    let _this = currentPages[currentPages.length - 1];
    let pagePath = _this.route;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for (let i in tabbar.list) {
      tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
  },
  async receiverUnRead() {
    var unRead = 0;
    let currentPages = getCurrentPages();
    let _this = currentPages[currentPages.length - 1];
    wx.onSocketMessage(async function (res) {
      console.log("收到服务器内容：" + res.data)
      if (res.data != '') {
        await wx.User.listContact()
        unRead = wx.getStorageSync('contact').unRead;
        _this.setData({
          unRead: unRead
        });
        console.log("unRead", unRead)
      }
    })
    await wx.User.listContact()
    unRead = wx.getStorageSync('contact').unRead;
    _this.setData({
      unRead: unRead
    });
    console.log("unRead", unRead)
  },
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.globalData.unRead = wx.getStorageSync('contact').unRead;
    this.globalData.tabBar = {
      "backgroundColor": "#fff",
      "color": "#979795",
      "selectedColor": "#1B7AFF",
      "list": [{
          "pagePath": "/pages/home/home",
          "text": "首页",
          "iconPath": "../../image/home1.png",
          "selectedIconPath": "../../image/home2.png"
        },
        {
          "pagePath": "/pages/publish/publish",
          "text": "发布",
          "iconPath": "../../image/publish1.png",
          "selectedIconPath": "../../image/publish2.png"
        },
        {
          "pagePath": "/pages/message/myMessage",
          "text": "消息",
          "iconPath": "../../image/message1.png",
          "selectedIconPath": "../../image/message2.png"
        },
        {
          "pagePath": "/pages/mine/mine",
          "text": "我的",
          "iconPath": "../../image/my1.png",
          "selectedIconPath": "../../image/my2.png"
        }
      ]
    }


    // 挂载全局方法
    wx.$http = request
    wx.User = user

    wx.User.__init()

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              wx.setStorageSync('userInfo', res.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null
  }
})